<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>React 登录注册逻辑 | 北城韩雨</title><meta name="keywords" content="React"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="React 登录注册逻辑">
<meta property="og:type" content="article">
<meta property="og:title" content="React 登录注册逻辑">
<meta property="og:url" content="http://example.com/2019/03/23/react%20%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="React 登录注册逻辑">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5">
<meta property="article:published_time" content="2019-03-22T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="React 实战">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2019/03/23/react%20%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'React 登录注册逻辑',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React 登录注册逻辑</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-22T16:00:00.000Z" title="发表于 2019-03-23 00:00:00">2019-03-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>37分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="React 登录注册逻辑"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="React-登录注册逻辑"><a href="#React-登录注册逻辑" class="headerlink" title="React 登录注册逻辑"></a>React 登录注册逻辑</h1><h2 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> create-react-app login-react-project</span><br><span class="line"><span class="number">2.</span> 删除了一些无用的文件如 app.css index.css等</span><br><span class="line"><span class="number">3.</span> 安装依赖 logger-redux(用来打印redux流程日志) redux-thunk(用来在redux中发送异步请求) redux(公共数据管理) react-reudx(redux便捷工具)</span><br></pre></td></tr></table></figure>
<h2 id="redux-和-reducer的配置"><a href="#redux-和-reducer的配置" class="headerlink" title="redux 和 reducer的配置"></a>redux 和 reducer的配置</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 创建一个reducer的文件夹 里面有 auth.js(管理权限的reducer)  和 index.js(所有reducer的汇总)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//auth.js 用来存放 权限的公共数据 比如 登录</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">auth</span>(<span class="params">state = &#123;&#125;,action</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//index.js 用来 合并所有的reducer的文件</span></span><br><span class="line"><span class="keyword">import</span> &#123;combineReducers&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;auth&#125; <span class="keyword">from</span> <span class="string">&#x27;./auth&#x27;</span></span><br><span class="line"><span class="keyword">const</span> root = combineReducers(&#123;</span><br><span class="line">    auth</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> root</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2.</span>最后 在启动文件里 也就是 src/index.jsx  配置 各种插件 配置 redux</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//src/index.js文件</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">&#x27;react-dom&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> reportWebVitals <span class="keyword">from</span> <span class="string">&#x27;./reportWebVitals&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> logger <span class="keyword">from</span> <span class="string">&#x27;redux-logger&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&#x27;redux-thunk&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;composeWithDevTools&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-devtools-extension&#x27;</span></span><br><span class="line"><span class="comment">//配置redux</span></span><br><span class="line"><span class="keyword">import</span>&#123;createStore,applyMiddleware&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="comment">//引入reducer</span></span><br><span class="line"><span class="keyword">import</span> rootRducer <span class="keyword">from</span> <span class="string">&#x27;./reducers&#x27;</span></span><br><span class="line"><span class="comment">// 挂载到 react-redux</span></span><br><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//配置redux</span></span><br><span class="line"><span class="keyword">const</span> store = createStore(rootRducer,composeWithDevTools(applyMiddleware(logger,thunk)))</span><br><span class="line"></span><br><span class="line"><span class="comment">//把我们配置好的 redux 和 我们的组件做关联</span></span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">    &lt;App /&gt;</span><br><span class="line">  &lt;/Provider&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">  </span><br><span class="line">);</span><br><span class="line">reportWebVitals();</span><br></pre></td></tr></table></figure>
<h2 id="路由的配置-amp-导航配置"><a href="#路由的配置-amp-导航配置" class="headerlink" title="路由的配置 &amp; 导航配置"></a>路由的配置 &amp; 导航配置</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们单独创建了一个routes.js文件 用来存放我们的路由文件</span><br><span class="line"><span class="number">2.</span> 这里引入了我们的注册页面 和 app 页面</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="comment">//引入路由组件</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入app 组件页面</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./components/App&#x27;</span></span><br><span class="line"><span class="comment">//引入 SignupPage(注册) 页面</span></span><br><span class="line"><span class="keyword">import</span> SignupPage <span class="keyword">from</span> <span class="string">&#x27;./components/signup/SignupPage&#x27;</span></span><br><span class="line"><span class="comment">//导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;Route path=<span class="string">&quot;/&quot;</span> exact component=&#123;App&#125;&gt;&lt;/Route&gt;   </span><br><span class="line">        &lt;Route path=<span class="string">&quot;/signup&quot;</span> exact component=&#123;SignupPage&#125;&gt;&lt;/Route&gt;   </span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3.</span> 配置我们的 导航 新建一个 NavigationBar.jsx  使用link 来配置我们的导航 这里我们使用了bootstrap样式</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Link&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Navigator</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;nav className=<span class="string">&quot;navbar navbar-expand-lg navbar-light bg-light mb-3&quot;</span>&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;container&quot;</span>&gt;</span><br><span class="line">                    &#123;<span class="comment">/*点击的login的时候 去 Login组件*/</span>&#125;</span><br><span class="line">                    &lt;Link className=<span class="string">&quot;navbar-brand&quot;</span> to=<span class="string">&quot;/&quot;</span>&gt;Login功能&lt;/Link&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&#x27;navbar-toggler&#x27;</span> type=<span class="string">&quot;button&quot;</span> data-toggle=<span class="string">&quot;collapse&quot;</span>&gt;</span><br><span class="line">                        &lt;span className=<span class="string">&quot;navbar-toggler-icon&quot;</span>&gt;&lt;/span&gt;</span><br><span class="line">                    &lt;/button&gt;</span><br><span class="line">                    &lt;div className=<span class="string">&quot;collapse navbar-collapse&quot;</span> id=<span class="string">&quot;navbarsExample05&quot;</span>&gt;</span><br><span class="line">                        &lt;ul className=<span class="string">&quot;navbar-nav mr-auto&quot;</span>&gt;</span><br><span class="line">                            &lt;li className=<span class="string">&quot;nav-item&quot;</span>&gt;</span><br><span class="line">                                &lt;Link className=<span class="string">&quot;nav-link&quot;</span> to=<span class="string">&quot;/signup&quot;</span>&gt;注册&lt;/Link&gt;</span><br><span class="line">                            &lt;/li&gt;</span><br><span class="line">                        &lt;/ul&gt;</span><br><span class="line">                    &lt;/div&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/nav&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span>  Navigator</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">4.</span> 在index.jsx(启动文件里) 引入我们的导航(Navigator) 和 我们路由(routes.js)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入路由模式文件</span></span><br><span class="line"><span class="keyword">import</span>&#123;BrowserRouter <span class="keyword">as</span> Router&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入routes文件</span></span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&#x27;./routes&#x27;</span></span><br><span class="line"><span class="comment">//引入菜单</span></span><br><span class="line"><span class="keyword">import</span> NavigationBar <span class="keyword">from</span> <span class="string">&#x27;./components/NavigationBar&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = createStore(rootRducer,composeWithDevTools(applyMiddleware(logger,thunk)))</span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">    &lt;Router routes=&#123;routes&#125;&gt;</span><br><span class="line">      &lt;NavigationBar/&gt;</span><br><span class="line">      &#123;routes&#125;</span><br><span class="line">    &lt;/Router&gt;</span><br><span class="line">  &lt;/Provider&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">  </span><br><span class="line">);</span><br><span class="line">reportWebVitals();</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5.</span> 效果如下 如切换如下</span><br></pre></td></tr></table></figure>
<p><img src="1.gif" alt=""></p>
<h2 id="注册页面"><a href="#注册页面" class="headerlink" title="注册页面"></a>注册页面</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们的注册页面是 signup/signupPage</span><br><span class="line"><span class="number">2.</span> signupPage.jsx 引入了 我们的表单组件 Signupform</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// signupPage.jsx</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="comment">// 表单的组件 </span></span><br><span class="line"><span class="keyword">import</span> Signupform <span class="keyword">from</span> <span class="string">&#x27;./Signupform&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SignupPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div className=<span class="string">&quot;row&quot;</span>&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-6&quot;</span>&gt;</span><br><span class="line">                &#123;<span class="comment">/* 引入了表单的组件 */</span>&#125;   </span><br><span class="line">                &lt;Signupform/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> SignupPage</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3.</span> 我们表单里的每一个input 都写了name属性 这样 我们在获取表单值的时候 就可以使用同一个onChange事件</span><br><span class="line"><span class="number">4.</span> 在设置我们state的时候 就可以使用 e.target.name : e.target.value 这种方式 赋值 就不需要写多个onChange事件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Signupform</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            username:<span class="string">&quot;&quot;</span>,</span><br><span class="line">            email:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            password:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            passwordConfirmation:<span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//每一个输入框都用同一个onchange事件</span></span><br><span class="line">    onChange = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            [e.target.name]:e.target.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    onSubmit = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        e.preventDefault()</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.state,<span class="string">&#x27;---&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;form onSubmit=&#123;<span class="built_in">this</span>.onSubmit&#125;&gt;</span><br><span class="line">                &lt;h1&gt;Join our community&lt;/h1&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Username&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     &#123;<span class="comment">/* 这里注意 要使用name属性去设置 方便我们才设置state的时候 使用同一个onChange事件 */</span>&#125; </span><br><span class="line">                     name=<span class="string">&quot;username&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.username&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=<span class="string">&quot;form-control&quot;</span></span><br><span class="line">                     /&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Email&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;email&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.email&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=<span class="string">&quot;form-control&quot;</span></span><br><span class="line">                     /&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Password&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.password&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=<span class="string">&quot;form-control&quot;</span></span><br><span class="line">                     /&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;PasswordConfirmation&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;passwordConfirmation&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.passwordConfirmation&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=<span class="string">&quot;form-control&quot;</span></span><br><span class="line">                     /&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&quot;btn btn-primary btn-lg&quot;</span>&gt;注册&lt;/button&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Signupform</span><br></pre></td></tr></table></figure>
<h3 id="注册的action配置"><a href="#注册的action配置" class="headerlink" title="注册的action配置"></a>注册的action配置</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先我们这里 和我们的 注册页面建立连接 是在父页面完成的 也就是 SignupPage </span><br><span class="line"><span class="number">2.</span> 然后把握们发送的action的函数 传递到了我们的 Signupform 页面里</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// actions文件夹下signupActions.js 这个文件里主要存放了我们各种格样的action   由于我们之前通过redux-thunk建立连接 所以 可以直接在里面写异步的操作比如发送请求 </span></span><br><span class="line"><span class="keyword">import</span> request <span class="keyword">from</span> <span class="string">&#x27;../utils/request&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> userSignupRquest = <span class="function">(<span class="params">userInfo</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">dispath</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> request(&#123;</span><br><span class="line">            url:<span class="string">&#x27;/api/users&#x27;</span>,</span><br><span class="line">            method:<span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">            data:userInfo</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Signupform <span class="keyword">from</span> <span class="string">&#x27;./Signupform&#x27;</span></span><br><span class="line"><span class="comment">//引入我们注册时候发送的action</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> signupActions <span class="keyword">from</span> <span class="string">&#x27;../../actions/signupActions&#x27;</span></span><br><span class="line"><span class="comment">//和redux 建立 连接</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125;<span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">// 把我们要发送的action 合并到一个 对象里</span></span><br><span class="line"><span class="keyword">import</span> &#123; bindActionCreators&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SignupPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div className=<span class="string">&quot;row&quot;</span> &gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-6&quot;</span>&gt;</span><br><span class="line">                &#123;<span class="comment">/* 把我们写好的action 传递给我们的form组件 这里由于我们的signuppage是被路由管理的 </span></span><br><span class="line"><span class="comment">                	当我们点击注册并且成功的时候 应该跳转到首页 这里我们直接把histoty传递下去 就可以</span></span><br><span class="line"><span class="comment">                	不在用路由组件包裹了</span></span><br><span class="line"><span class="comment">                */</span>&#125;</span><br><span class="line">                &lt;Signupform history=&#123;<span class="built_in">this</span>.props.history&#125; signupActions=&#123;<span class="built_in">this</span>.props.signupActions&#125;/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//把我们的action 挂载到 我们的porps上</span></span><br><span class="line"><span class="keyword">const</span> mapDispathToProps = <span class="function">(<span class="params">dispath</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        signupActions:bindActionCreators(signupActions,dispath)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//和我们的redux 建立连接</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="literal">null</span>,mapDispathToProps)(SignupPage)</span><br></pre></td></tr></table></figure>
<h3 id="注册流程"><a href="#注册流程" class="headerlink" title="注册流程"></a>注册流程</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 点击注册按钮的时候 我们去向后台数据发送请求 </span><br><span class="line"><span class="number">2.</span> 由于我们input框绑定了相同的change事件 所以 直接 就可以把state的值发送过去</span><br><span class="line"><span class="number">3.</span> 后台 如果验证失败 就会给我们返回错误信息 这个时候我们需要这些错误信息去显示在input框下面</span><br><span class="line"><span class="number">4.</span> 然后利用classnames的库 让文本框变红</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// signupform.jsx</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> classnames <span class="keyword">from</span> <span class="string">&#x27;classnames&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Signupform</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            username:<span class="string">&quot;&quot;</span>,</span><br><span class="line">            email:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            password:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            passwordConfirmation:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="comment">//注册错误的时候 接收后台返回来的错误</span></span><br><span class="line">            errors:&#123;&#125;,</span><br><span class="line">            <span class="comment">//控制注册按钮置灰状态 防止多重点击</span></span><br><span class="line">            isLoading:<span class="literal">false</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//每一个输入框都用同一个onchange事件</span></span><br><span class="line">    onChange = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            [e.target.name]:e.target.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    onSubmit = <span class="keyword">async</span> (e) =&gt; &#123;</span><br><span class="line">        e.preventDefault()</span><br><span class="line">        <span class="comment">//控制按钮置灰</span></span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            isLoading:<span class="literal">true</span></span><br><span class="line">        &#125;)</span><br><span class="line">         <span class="keyword">const</span> &#123;data&#125; = <span class="keyword">await</span> <span class="built_in">this</span>.props.signupActions.userSignupRquest(<span class="built_in">this</span>.state)</span><br><span class="line">         <span class="keyword">if</span>(data.code !== <span class="number">200</span>)&#123;</span><br><span class="line">             <span class="comment">//判断是否验证不通过 然后 设置错误信息 用于页面的显示</span></span><br><span class="line">             <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                errors:data.errors,</span><br><span class="line">                isLoading:<span class="literal">false</span></span><br><span class="line">             &#125;)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> &#123;username,email,password,passwordConfirmation&#125; = <span class="built_in">this</span>.state.errors</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;form onSubmit=&#123;<span class="built_in">this</span>.onSubmit&#125;&gt;</span><br><span class="line">                &lt;h1&gt;Join our community&lt;/h1&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Username&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;username&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.username&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     &#123;<span class="comment">/* 这里用 classnames的库 来判断 如果返回的错误信息有 username 我们就在添加个类</span></span><br><span class="line"><span class="comment">                     is-invalid 这个类 可以是文本框变红</span></span><br><span class="line"><span class="comment">                     */</span>&#125;   </span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:username&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                    &#123;<span class="comment">/* 在把错误信息显示出来 */</span>&#125; </span><br><span class="line">                     &#123;username &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;username&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Email&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;email&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.email&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:email&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;email &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;email&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Password&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.password&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:password&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;password &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;password&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;PasswordConfirmation&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;passwordConfirmation&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.passwordConfirmation&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:passwordConfirmation&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;passwordConfirmation &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;passwordConfirmation&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&quot;btn btn-primary btn-lg&quot;</span> disabled=&#123;<span class="built_in">this</span>.state.isLoading&#125;&gt;注册&lt;/button&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Signupform</span><br></pre></td></tr></table></figure>
<p><img src="2.gif" alt=""></p>
<h2 id="注册成功或失败的提示"><a href="#注册成功或失败的提示" class="headerlink" title="注册成功或失败的提示"></a>注册成功或失败的提示</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 首先 我们会有一个消息的列表 这个消息列表的数据 是从reducer里取的</span><br><span class="line"><span class="number">2.</span> 消息列表中的数据 是我们在点击注册之后 服务器返回成功或者失败 的消息之后 我们发送action 然后把消息存放在reducer中</span><br><span class="line"><span class="number">3.</span> 由于我们的注册页面 表单 和 注册页面是相分离的 所以 我们的action方法 是传递到form组件中的</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">4.</span> 首先我们创建reducer 用去存放我们的消息列表</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// reducers 文件夹下 flashMessages.js </span></span><br><span class="line"><span class="keyword">import</span> &#123; ADD_FLASH_MESSAGE&#125; <span class="keyword">from</span> <span class="string">&#x27;../constants&#x27;</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">0</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> flashMessages = <span class="function">(<span class="params">state = [],action = &#123;&#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> ADD_FLASH_MESSAGE:</span><br><span class="line">            <span class="keyword">return</span> [</span><br><span class="line">                ...state,</span><br><span class="line">                &#123;</span><br><span class="line">                    &#123;<span class="comment">/* 用于删除使用 */</span>&#125;</span><br><span class="line">                    id:num++,</span><br><span class="line">                    type:action.message.type,</span><br><span class="line">                    text:action.message.text</span><br><span class="line">                &#125;</span><br><span class="line">            ]</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 合并我们的reducer reducers文件下的 index.js</span></span><br><span class="line"><span class="keyword">import</span> &#123;combineReducers&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;auth&#125; <span class="keyword">from</span> <span class="string">&#x27;./auth&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;flashMessages&#125; <span class="keyword">from</span> <span class="string">&#x27;./flashMessages&#x27;</span></span><br><span class="line"><span class="keyword">const</span> root = combineReducers(&#123;</span><br><span class="line">    auth,</span><br><span class="line">    flashMessages</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> root</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5.</span> 创建发送action的文件 </span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// actions 文件夹下 flashMessages.js 用来发送action</span></span><br><span class="line"><span class="keyword">import</span> &#123; ADD_FLASH_MESSAGE &#125; <span class="keyword">from</span> <span class="string">&#x27;../constants&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> addFlshMessage = <span class="function">(<span class="params">message</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:ADD_FLASH_MESSAGE,</span><br><span class="line">        message</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6.</span> 把我们的action文件 引入我们的signupPage.jsx 在把这个发射action的方法 传递给我们的signupform.jsx </span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Signupform <span class="keyword">from</span> <span class="string">&#x27;./Signupform&#x27;</span></span><br><span class="line"><span class="comment">// 我们注册的时候 发送的网路请求 action</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> signupActions <span class="keyword">from</span> <span class="string">&#x27;../../actions/signupActions&#x27;</span></span><br><span class="line"><span class="comment">// 注册成功 或者 错误的 消息列表action</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> flashActions <span class="keyword">from</span> <span class="string">&#x27;../../actions/flashMessages&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125;<span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; bindActionCreators&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SignupPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div className=<span class="string">&quot;row&quot;</span> &gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-6&quot;</span>&gt;</span><br><span class="line">                &#123;<span class="comment">/* 这里就把这两个action都传递给我们的 Signupform 组件 当点击注册时候会触发这两个*/</span>&#125;    </span><br><span class="line">                &lt;Signupform history=&#123;<span class="built_in">this</span>.props.history&#125; signupActions=&#123;<span class="built_in">this</span>.props.signupActions&#125; flashActions=&#123;<span class="built_in">this</span>.props.flashActions&#125;/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mapDispathToProps = <span class="function">(<span class="params">dispath</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        signupActions:bindActionCreators(signupActions,dispath),</span><br><span class="line">        flashActions:bindActionCreators(flashActions,dispath)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="literal">null</span>,mapDispathToProps)(SignupPage)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">7.</span> 我们注册成功或者失败的消息列表 组件 一开始就要去reducer里去取 消息列表 所以我们创建了 flashMessagesList 组件 他其中 有 flashMessages组件 就是我们的消息列表</span><br><span class="line"><span class="number">8.</span> flashMessagesList去reducer里面取出消息列表 然后在 传递给我们的 flashMessages 去显示</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//flashMessagesList.jsx 组件</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="comment">//消息列表单个组件 由于我们的消息列表是个数组 所以需要循环出来 然后传递给我们的列表组件的每一项</span></span><br><span class="line"><span class="keyword">import</span> FlashMessage <span class="keyword">from</span><span class="string">&#x27;./FlashMessage&#x27;</span></span><br><span class="line"><span class="comment">// 和我们的redux 建立连接</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FlashMessagesList</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//由于我们的注册成功或者失败的消息 是个列表 所以我们要循环出来</span></span><br><span class="line">        <span class="keyword">const</span> messages = <span class="built_in">this</span>.props.messages.map(<span class="function"><span class="params">message</span> =&gt;</span> </span><br><span class="line">            &lt;FlashMessage key=&#123;message.id&#125; message=&#123; message&#125;/&gt;</span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div className=<span class="string">&quot;container&quot;</span>&gt;</span><br><span class="line">                &#123;messages&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//去reducer里取我们循环列表 </span></span><br><span class="line"><span class="keyword">const</span> mapStateToProps = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span>&#123;</span><br><span class="line">        messages:state.flashMessages</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(mapStateToProps)(FlashMessagesList)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">9.</span> 最后把我们的 FlashMessagesList 组件挂载到我们的index.jsx文件夹上 </span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入路由模式文件</span></span><br><span class="line"><span class="keyword">import</span>&#123;BrowserRouter <span class="keyword">as</span> Router&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入router文件</span></span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&#x27;./routes&#x27;</span></span><br><span class="line"><span class="comment">//引入菜单</span></span><br><span class="line"><span class="keyword">import</span> NavigationBar <span class="keyword">from</span> <span class="string">&#x27;./components/NavigationBar&#x27;</span></span><br><span class="line"><span class="comment">//引入消息提示列表</span></span><br><span class="line"><span class="keyword">import</span> FlashMessagesList <span class="keyword">from</span> <span class="string">&#x27;./components/flash/FlashMessagesList&#x27;</span></span><br><span class="line"><span class="keyword">const</span> store = createStore(rootRducer,composeWithDevTools(applyMiddleware(logger,thunk)))</span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">    &lt;Router routes=&#123;routes&#125;&gt;</span><br><span class="line">      &lt;NavigationBar/&gt;</span><br><span class="line">      &lt;FlashMessagesList /&gt;</span><br><span class="line">      &#123;routes&#125;</span><br><span class="line">    &lt;/Router&gt;</span><br><span class="line">  &lt;/Provider&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">  </span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="删除提示"><a href="#删除提示" class="headerlink" title="删除提示"></a>删除提示</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 因为我们的成功或者失败的消息列表 是 在reducer里存放 所以我们需要发送action</span><br><span class="line"><span class="number">2.</span> 这里 我们还是需要把我们的 action 传入到我们的 FlashMessage 组件里面去 </span><br><span class="line"><span class="number">3.</span> 当reducer 接收到我们发送的删除action 然后去我们的state里做删除操作</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// action </span></span><br><span class="line"><span class="keyword">import</span> &#123; ADD_FLASH_MESSAGE,DEL_FLASH_MESSAGE &#125; <span class="keyword">from</span> <span class="string">&#x27;../constants&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> addFlshMessage = <span class="function">(<span class="params">message</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:ADD_FLASH_MESSAGE,</span><br><span class="line">        message</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//删除的操作</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> delFlshMessage = <span class="function">(<span class="params">id</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//这里不用些dispath直接</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        type:DEL_FLASH_MESSAGE,</span><br><span class="line">        id</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// FlashMessagesList 文件 把我们的引入进来 然后 写入我们的connect 函数里面</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> FlashMessage <span class="keyword">from</span><span class="string">&#x27;./FlashMessage&#x27;</span></span><br><span class="line"><span class="comment">//这里是我们删除的action</span></span><br><span class="line"><span class="keyword">import</span> &#123;delFlshMessage&#125; <span class="keyword">from</span> <span class="string">&#x27;../../actions/flashMessages&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FlashMessagesList</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> messages = <span class="built_in">this</span>.props.messages.map(<span class="function"><span class="params">message</span> =&gt;</span> </span><br><span class="line">            <span class="comment">//把删除的action传入到了我们的 FlashMessage 点击X的时候 去发送</span></span><br><span class="line">            &lt;FlashMessage key=&#123;message.id&#125; message=&#123; message&#125; delFlshMessage=&#123;<span class="built_in">this</span>.props.delFlshMessage&#125;/&gt;</span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div className=<span class="string">&quot;container&quot;</span>&gt;</span><br><span class="line">                &#123;messages&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mapStateToProps = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span>&#123;</span><br><span class="line">        messages:state.flashMessages</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这里直接解构出来就可以使用</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(mapStateToProps,&#123;delFlshMessage&#125;)(FlashMessagesList)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// FlashMessage 组件 当我们点击X的时候 去发送action</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> classnames <span class="keyword">from</span> <span class="string">&#x27;classnames&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FlashMessage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    onClick = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">//点击X的时候去发送action 这里的id就是我们传入进来的</span></span><br><span class="line">        <span class="built_in">this</span>.props.delFlshMessage(<span class="built_in">this</span>.props.message.id)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> &#123;type,text&#125; = <span class="built_in">this</span>.props.message</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;div className=&#123;classnames(<span class="string">&#x27;alert&#x27;</span>,&#123;</span><br><span class="line">                <span class="string">&#x27;alert-success&#x27;</span>: type === <span class="string">&#x27;success&#x27;</span>,</span><br><span class="line">                <span class="string">&#x27;alert-danger&#x27;</span>: type === <span class="string">&#x27;danger&#x27;</span></span><br><span class="line">            &#125;)&#125;&gt;</span><br><span class="line">                &lt;button onClick=&#123;<span class="built_in">this</span>.onClick&#125; className=<span class="string">&quot;close&quot;</span>&gt;&amp;times;&lt;/button&gt;</span><br><span class="line">                &#123;text&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> FlashMessage</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// flashMessages.js reducer 监视到我们发送的删除的action 然后做对应的删除操作</span></span><br><span class="line"><span class="keyword">import</span> &#123; ADD_FLASH_MESSAGE,DEL_FLASH_MESSAGE&#125; <span class="keyword">from</span> <span class="string">&#x27;../constants&#x27;</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">1</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> flashMessages = <span class="function">(<span class="params">state = [],action = &#123;&#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> ADD_FLASH_MESSAGE:</span><br><span class="line">            <span class="keyword">return</span> [</span><br><span class="line">                ...state,</span><br><span class="line">                &#123;</span><br><span class="line">                    id:num++,</span><br><span class="line">                    type:action.message.type,</span><br><span class="line">                    text:action.message.text</span><br><span class="line">                &#125;</span><br><span class="line">            ]</span><br><span class="line">         <span class="comment">//找到ID 然后 做删除 在返回给我们的视图   </span></span><br><span class="line">         <span class="keyword">case</span>  DEL_FLASH_MESSAGE: </span><br><span class="line">         state = state.filter(<span class="function"><span class="params">item</span> =&gt;</span> action.id !== item.id)</span><br><span class="line">         <span class="keyword">return</span>  [...state]</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//以上的整个删除流程就完毕了</span></span><br></pre></td></tr></table></figure>
<h2 id="输入用户名失去焦点的时候验证"><a href="#输入用户名失去焦点的时候验证" class="headerlink" title="输入用户名失去焦点的时候验证"></a>输入用户名失去焦点的时候验证</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 当我们在注册页面的时候  我们当输入完用户名之后 回去验证 看看 此用户名有没有被注册</span><br><span class="line"><span class="number">2.</span> 这个动作也是在action里完成的</span><br><span class="line"><span class="number">3.</span> 我们这里还是需要在signform 页面里面 把我们action 传入给我们的 signLogin </span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// action / signupActions 文件 </span></span><br><span class="line"><span class="keyword">import</span> request <span class="keyword">from</span> <span class="string">&#x27;../utils/request&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> userSignupRquest = <span class="function">(<span class="params">userInfo</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">dispath</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> request(&#123;</span><br><span class="line">            url:<span class="string">&#x27;/api/users&#x27;</span>,</span><br><span class="line">            method:<span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">            data:userInfo</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//当失去焦点的时候 要触发这个action</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> isUserExists = <span class="function">(<span class="params">username</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">dispath</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> request(&#123;</span><br><span class="line">            url:<span class="string">`/api/users/<span class="subst">$&#123;username&#125;</span>`</span>,</span><br><span class="line">            method:<span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// SignupPage 组件 通过react-redux把我们的action 与 redux做连接 然后传入到我们的 signForm组件</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Signupform <span class="keyword">from</span> <span class="string">&#x27;./Signupform&#x27;</span></span><br><span class="line"><span class="comment">//引入了我们的失去焦点验证的action</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> signupActions <span class="keyword">from</span> <span class="string">&#x27;../../actions/signupActions&#x27;</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> flashActions <span class="keyword">from</span> <span class="string">&#x27;../../actions/flashMessages&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125;<span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; bindActionCreators&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SignupPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div className=<span class="string">&quot;row&quot;</span> &gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-6&quot;</span>&gt;</span><br><span class="line">                &#123;<span class="comment">/*这里把失去焦点的请求传入到了我们的 singForm文件里*/</span>&#125;   </span><br><span class="line">                &lt;Signupform history=&#123;<span class="built_in">this</span>.props.history&#125; signupActions=&#123;<span class="built_in">this</span>.props.signupActions&#125; flashActions=&#123;<span class="built_in">this</span>.props.flashActions&#125;/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-md-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> mapDispathToProps = <span class="function">(<span class="params">dispath</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="comment">//把我们的失去焦点验证的请求挂载到了 props 上 </span></span><br><span class="line">        signupActions:bindActionCreators(signupActions,dispath),</span><br><span class="line">        flashActions:bindActionCreators(flashActions,dispath)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="literal">null</span>,mapDispathToProps)(SignupPage)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// signForm 页面 </span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> classnames <span class="keyword">from</span> <span class="string">&#x27;classnames&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Signupform</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            username:<span class="string">&quot;&quot;</span>,</span><br><span class="line">            email:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            password:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            passwordConfirmation:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="comment">//注册错误的时候 接收后台返回来的错误</span></span><br><span class="line">            errors:&#123;&#125;,</span><br><span class="line">            <span class="comment">//控制注册按钮置灰状态 防止多重点击</span></span><br><span class="line">            isLoading:<span class="literal">false</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//每一个输入框都用同一个onchange事件</span></span><br><span class="line">    onChange = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            [e.target.name]:e.target.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    checkUser = <span class="keyword">async</span> (e) =&gt; &#123;</span><br><span class="line">        <span class="comment">//拿到我的表单的key 也就是username</span></span><br><span class="line">        <span class="keyword">const</span> field = e.target.name</span><br><span class="line">        <span class="keyword">const</span> val = e.target.value</span><br><span class="line">        <span class="keyword">if</span>(val !== <span class="string">&#x27;&#x27;</span>)&#123;</span><br><span class="line">           <span class="comment">//去发送了请求  </span></span><br><span class="line">           <span class="keyword">const</span> &#123;data&#125; = <span class="keyword">await</span> <span class="built_in">this</span>.props.signupActions.isUserExists(val)</span><br><span class="line">           <span class="keyword">let</span> errors = <span class="built_in">this</span>.state.errors</span><br><span class="line">           <span class="keyword">let</span> isLoading = <span class="built_in">this</span>.state.isLoading</span><br><span class="line">           <span class="comment">//如果后台有数据返回来 就说明有人注册了 给与对应的提示和错误提示</span></span><br><span class="line">           <span class="keyword">if</span>(data[<span class="number">0</span>])&#123;</span><br><span class="line">                errors[field] = <span class="string">`用户名存在:<span class="subst">$&#123;field&#125;</span>`</span></span><br><span class="line">                isLoading = <span class="literal">true</span></span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                errors[field] = <span class="string">&#x27;&#x27;</span></span><br><span class="line">                isLoading = <span class="literal">false</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                errors,</span><br><span class="line">                isLoading</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    onSubmit = <span class="keyword">async</span> (e) =&gt; &#123;</span><br><span class="line">        e.preventDefault()</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            isLoading:<span class="literal">true</span></span><br><span class="line">        &#125;)</span><br><span class="line">         <span class="keyword">const</span> &#123;data&#125; = <span class="keyword">await</span> <span class="built_in">this</span>.props.signupActions.userSignupRquest(<span class="built_in">this</span>.state)</span><br><span class="line">         <span class="keyword">if</span>(data.code !== <span class="number">200</span>)&#123;</span><br><span class="line">             <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                errors:data.errors,</span><br><span class="line">                isLoading:<span class="literal">false</span></span><br><span class="line">             &#125;)</span><br><span class="line">             <span class="built_in">this</span>.props.flashActions.addFlshMessage(&#123;</span><br><span class="line">                 type:<span class="string">&#x27;danger&#x27;</span>,</span><br><span class="line">                 text:<span class="string">&#x27;注册失败,请重新注册!&#x27;</span></span><br><span class="line">             &#125;)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">         &#125;</span><br><span class="line">         <span class="built_in">this</span>.props.flashActions.addFlshMessage(&#123;</span><br><span class="line">            type:<span class="string">&#x27;success&#x27;</span>,</span><br><span class="line">            text:<span class="string">&#x27;注册成功!&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">         <span class="built_in">this</span>.props.history.push(<span class="string">&#x27;/&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> &#123;username,email,password,passwordConfirmation&#125; = <span class="built_in">this</span>.state.errors</span><br><span class="line">        <span class="keyword">return</span>(</span><br><span class="line">            &lt;form onSubmit=&#123;<span class="built_in">this</span>.onSubmit&#125;&gt;</span><br><span class="line">                &lt;h1&gt;Join our community&lt;/h1&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Username&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;username&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.username&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     &#123;<span class="comment">/*失去焦点的时候去触发请求*/</span>&#125;   </span><br><span class="line">                     onBlur=&#123;<span class="built_in">this</span>.checkUser&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:username&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;username &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;username&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Email&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;text&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;email&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.email&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:email&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;email &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;email&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Password&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.password&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:password&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;password &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;password&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;PasswordConfirmation&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                     type=<span class="string">&quot;password&quot;</span> </span><br><span class="line">                     name=<span class="string">&quot;passwordConfirmation&quot;</span> </span><br><span class="line">                     value=&#123;<span class="built_in">this</span>.state.passwordConfirmation&#125;</span><br><span class="line">                     onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                     className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>,&#123;<span class="string">&#x27;is-invalid&#x27;</span>:passwordConfirmation&#125;)&#125;</span><br><span class="line">                     /&gt;</span><br><span class="line">                     &#123;passwordConfirmation &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;passwordConfirmation&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&quot;btn btn-primary btn-lg&quot;</span> disabled=&#123;<span class="built_in">this</span>.state.isLoading&#125;&gt;注册&lt;/button&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Signupform</span><br></pre></td></tr></table></figure>
<h2 id="登录"><a href="#登录" class="headerlink" title="登录"></a>登录</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 登录的时候 后台会返回给我们token </span><br><span class="line"><span class="number">2.</span> 这个token 我们需要存放在 本地存储一个 还有reducer里一个 </span><br><span class="line"><span class="number">3.</span> 这个token 我们需要用 jwt-token插件 去解密出来 生成我们的用户信息</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; SET_CURRENT_USER &#125; <span class="keyword">from</span> <span class="string">&#x27;../constants&#x27;</span></span><br><span class="line"><span class="keyword">import</span> request <span class="keyword">from</span> <span class="string">&#x27;../utils/request&#x27;</span></span><br><span class="line"><span class="comment">//请求token的action</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> login = <span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function">(<span class="params">dispatch</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> request(&#123;</span><br><span class="line">            url:<span class="string">&#x27;/api/auth&#x27;</span>,</span><br><span class="line">            method:<span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">            data</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//存放token的action</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> serCurrentuser = <span class="function">(<span class="params">user</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span>&#123;</span><br><span class="line">        type:SET_CURRENT_USER,</span><br><span class="line">        user</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点击退出的时候需要移除的token</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> logout = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">dispath</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">//然后还需要再次发送我们的action 去把我们的登录信息设置为false 这里就直接传个空对象就好了</span></span><br><span class="line">        <span class="built_in">localStorage</span>.removeItem(<span class="string">&#x27;jwtToken&#x27;</span>)</span><br><span class="line">        dispath(serCurrentuser(&#123;&#125;))</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// loginPage 把我们上面写好的action传入到我们的loginForm文件里</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> LoginForm <span class="keyword">from</span> <span class="string">&#x27;./LoginForm&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">//引入了我们的action</span></span><br><span class="line"><span class="keyword">import</span> &#123;login&#125;<span class="keyword">from</span> <span class="string">&#x27;../../actions/login&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginPage</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;div className=<span class="string">&quot;row&quot;</span>&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-sm-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-sm-6&quot;</span>&gt;</span><br><span class="line">            		&#123;<span class="comment">/*把我们的传入到我们的login组件里*/</span>&#125;</span><br><span class="line">                    &lt;LoginForm login=&#123;<span class="built_in">this</span>.props.login&#125;/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;col-sm-3&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//和redux 做连接</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(<span class="literal">null</span>,&#123;login&#125;)(LoginPage)</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// loginform组件 登录的时候 触发action 这里还有一些验证的函数 不在写逻辑了</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> classnames <span class="keyword">from</span> <span class="string">&#x27;classnames&#x27;</span></span><br><span class="line"><span class="keyword">import</span> validateInput <span class="keyword">from</span> <span class="string">&#x27;../../utils/validators/login&#x27;</span></span><br><span class="line"><span class="keyword">import</span>&#123;withRouter&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//因为这里发送了登录的请求之后 我们还需要发送一个action 这里相当于写了两次 以后在改吧</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> jwtDecode <span class="keyword">from</span> <span class="string">&#x27;jwt-decode&#x27;</span></span><br><span class="line"><span class="comment">//这个是我们存放token的action</span></span><br><span class="line"><span class="keyword">import</span> &#123;serCurrentuser&#125; <span class="keyword">from</span> <span class="string">&#x27;../../actions/login&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">props</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(props)</span><br><span class="line">        <span class="built_in">this</span>.state = &#123;</span><br><span class="line">            username: <span class="string">&quot;&quot;</span>,</span><br><span class="line">            password: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="comment">//注册错误的时候 接收后台返回来的错误</span></span><br><span class="line">            errors: &#123;&#125;,</span><br><span class="line">            <span class="comment">//控制注册按钮置灰状态 防止多重点击</span></span><br><span class="line">            isLoading: <span class="literal">false</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    onChange = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            [e.target.name]: e.target.value</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//验证逻辑</span></span><br><span class="line">    validate = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; errors, isvalid &#125; = validateInput(<span class="built_in">this</span>.state)</span><br><span class="line">        <span class="keyword">if</span> (!isvalid) &#123;</span><br><span class="line">            <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                errors</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> isvalid</span><br><span class="line">    &#125;</span><br><span class="line">    onSubmit = <span class="keyword">async</span> (e) =&gt; &#123;</span><br><span class="line">        e.preventDefault()</span><br><span class="line">        <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">            isLoading: <span class="literal">true</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.validate()) &#123;</span><br><span class="line">            <span class="comment">//我们触发第一个这里会拿回来token</span></span><br><span class="line">            <span class="keyword">const</span> &#123; data &#125; = <span class="keyword">await</span> <span class="built_in">this</span>.props.login(<span class="built_in">this</span>.state)</span><br><span class="line">            <span class="built_in">console</span>.log(data)</span><br><span class="line">            <span class="keyword">if</span> (data.token) &#123;</span><br><span class="line">                <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                    isLoading: <span class="literal">false</span></span><br><span class="line">                &#125;)</span><br><span class="line">                <span class="comment">//首先 把它存放在 本地存储</span></span><br><span class="line">                <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;jwtToken&#x27;</span>,data.token)</span><br><span class="line">                <span class="comment">//然后在发送action 存放我们请求回来的token 存放之前需要把token转换称我们的用户信息</span></span><br><span class="line">                <span class="built_in">this</span>.props.serCurrentuser(jwtDecode(data.token))</span><br><span class="line">                <span class="built_in">this</span>.props.history.push(<span class="string">&#x27;/&#x27;</span>)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">this</span>.setState(&#123;</span><br><span class="line">                    isLoading: <span class="literal">false</span>,</span><br><span class="line">                    errors: &#123;<span class="attr">errors</span>:data.errors&#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; errors &#125; = <span class="built_in">this</span>.state</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;form onSubmit=&#123;<span class="built_in">this</span>.onSubmit&#125;&gt;</span><br><span class="line">                &lt;h1&gt;Login&lt;/h1&gt;</span><br><span class="line">                &#123;errors.errors &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;alert alert-danger&quot;</span>&gt;</span>&#123;errors.errors&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>&#125;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Username&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                        type=<span class="string">&quot;text&quot;</span></span><br><span class="line">                        name=<span class="string">&quot;username&quot;</span></span><br><span class="line">                        value=&#123;<span class="built_in">this</span>.state.username&#125;</span><br><span class="line">                        onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                        className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>, &#123; <span class="string">&#x27;is-invalid&#x27;</span>: errors.username &#125;)&#125;</span><br><span class="line">                    /&gt;</span><br><span class="line">                    &#123;errors.username &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;errors.username&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                    &lt;label className=<span class="string">&quot;control-label&quot;</span>&gt;Password&lt;/label&gt;</span><br><span class="line">                    &lt;input</span><br><span class="line">                        type=<span class="string">&quot;password&quot;</span></span><br><span class="line">                        name=<span class="string">&quot;password&quot;</span></span><br><span class="line">                        value=&#123;<span class="built_in">this</span>.state.password&#125;</span><br><span class="line">                        onChange=&#123;<span class="built_in">this</span>.onChange&#125;</span><br><span class="line">                        className=&#123;classnames(<span class="string">&#x27;form-control&#x27;</span>, &#123; <span class="string">&#x27;is-invalid&#x27;</span>: errors.password &#125;)&#125;</span><br><span class="line">                    /&gt;</span><br><span class="line">                    &#123;errors.password &amp;&amp; <span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">className</span>=<span class="string">&quot;form-text text-muted&quot;</span>&gt;</span>&#123;errors.password&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>&#125;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;form-group&quot;</span>&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&quot;btn btn-primary btn-lg&quot;</span> disabled=&#123;<span class="built_in">this</span>.state.isLoading&#125;&gt;Login&lt;/button&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(connect(<span class="literal">null</span>,&#123;serCurrentuser&#125;)(LoginForm))</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 对应login的reducer</span></span><br><span class="line"><span class="keyword">import</span> &#123;SET_CURRENT_USER&#125; <span class="keyword">from</span> <span class="string">&#x27;../constants/index&#x27;</span></span><br><span class="line"><span class="keyword">import</span> isEmpty <span class="keyword">from</span> <span class="string">&#x27;lodash/isEmpty&#x27;</span></span><br><span class="line"><span class="keyword">import</span> jwtDecode <span class="keyword">from</span> <span class="string">&#x27;jwt-decode&#x27;</span></span><br><span class="line"><span class="comment">//这里因为</span></span><br><span class="line"><span class="keyword">const</span> initalState = &#123;</span><br><span class="line">    <span class="comment">//判断你是否登录 一会用来控制 导航的动态显示</span></span><br><span class="line">    isAuth:<span class="literal">false</span>,</span><br><span class="line">    <span class="comment">//如果刷新了 我们的reducer就没有了 所以我们需要在去本地存储去token 使用 jwtDecode 转换为我们的用户信息</span></span><br><span class="line">    user:<span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;jwtToken&#x27;</span>)?jwtDecode(<span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;jwtToken&#x27;</span>)):&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">auth</span>(<span class="params">state = initalState,action</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//当我们点击登录的时候 serCurrentuser这个action方法 就对应走到了下面的</span></span><br><span class="line">    <span class="keyword">switch</span>(action.type)&#123;</span><br><span class="line">        <span class="keyword">case</span> SET_CURRENT_USER:</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            isAuth: !isEmpty(action.user),</span><br><span class="line">            user:action.user</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">return</span> state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这里在刷新的的时候 reducer里的数据就没有了 所以我们在启动文件里 也需要触发一下我们存放token的action</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">&#x27;react-dom&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> reportWebVitals <span class="keyword">from</span> <span class="string">&#x27;./reportWebVitals&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> logger <span class="keyword">from</span> <span class="string">&#x27;redux-logger&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> thunk <span class="keyword">from</span> <span class="string">&#x27;redux-thunk&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;composeWithDevTools&#125; <span class="keyword">from</span> <span class="string">&#x27;redux-devtools-extension&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span>&#123;createStore,applyMiddleware&#125; <span class="keyword">from</span> <span class="string">&#x27;redux&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> rootRducer <span class="keyword">from</span> <span class="string">&#x27;./reducers&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">//引入存放token的action</span></span><br><span class="line"><span class="keyword">import</span>&#123; serCurrentuser&#125; <span class="keyword">from</span> <span class="string">&#x27;./actions/login&#x27;</span></span><br><span class="line"><span class="comment">//把token 转换成我们的用户信息 比如username之类的</span></span><br><span class="line"><span class="keyword">import</span> jwtDecode <span class="keyword">from</span> <span class="string">&#x27;jwt-decode&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//引入路由模式文件</span></span><br><span class="line"><span class="keyword">import</span>&#123;BrowserRouter <span class="keyword">as</span> Router&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//引入router文件</span></span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&#x27;./routes&#x27;</span></span><br><span class="line"><span class="comment">//引入菜单</span></span><br><span class="line"><span class="keyword">import</span> NavigationBar <span class="keyword">from</span> <span class="string">&#x27;./components/NavigationBar&#x27;</span></span><br><span class="line"><span class="comment">//引入消息提示列表</span></span><br><span class="line"><span class="keyword">import</span> FlashMessagesList <span class="keyword">from</span> <span class="string">&#x27;./components/flash/FlashMessagesList&#x27;</span></span><br><span class="line"><span class="keyword">const</span> store = createStore(rootRducer,composeWithDevTools(applyMiddleware(logger,thunk)))</span><br><span class="line"><span class="comment">//先看看 能不能取出来如果取出来了 就说明你登录过了 我们就去 发射存放token的action </span></span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;jwtToken&#x27;</span>))&#123;</span><br><span class="line">  store.dispatch(serCurrentuser(jwtDecode(<span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;jwtToken&#x27;</span>))))</span><br><span class="line">&#125;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Provider store=&#123;store&#125;&gt;</span><br><span class="line">    &lt;Router routes=&#123;routes&#125;&gt;</span><br><span class="line">      &lt;NavigationBar/&gt;</span><br><span class="line">      &lt;FlashMessagesList /&gt;</span><br><span class="line">      &#123;routes&#125;</span><br><span class="line">    &lt;/Router&gt;</span><br><span class="line">  &lt;/Provider&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">  </span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h2 id="导航的动态显示"><a href="#导航的动态显示" class="headerlink" title="导航的动态显示"></a>导航的动态显示</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们现在的导航是注册 登录 </span><br><span class="line"><span class="number">2.</span> 如果我们登录了 导航 就应该显示 退出</span><br><span class="line"><span class="number">3.</span> 刚刚我们的auth.js 这个reducer 已经设置了是否登录的标识 所以我们用它判断就好了</span><br><span class="line"><span class="number">4.</span> 当我们点击退出的时候 我们还需要发送的对应的action 去清除我们的本地存储还有我们的reducer 上面已经写过了</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// NavigationBar</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; Link &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;logout&#125; <span class="keyword">from</span> <span class="string">&#x27;../actions/login&#x27;</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Navigator</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">    <span class="comment">//点击退出的时候 可以 触发我们的logout action 主要是为了清除一下我们的reducer 和本地存储</span></span><br><span class="line">    <span class="function"><span class="title">logout</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">        e.preventDefault()</span><br><span class="line">        <span class="built_in">this</span>.props.logout()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">render</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//这里就开始判断 如果你登录 导航就只给你显示退出 如果没有登录 会给你显示 注册 登录的导航</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.props.auth,<span class="string">&#x27;=---&#x27;</span>)</span><br><span class="line">        <span class="keyword">const</span> isAuth = <span class="built_in">this</span>.props.auth.isAuth</span><br><span class="line">        <span class="keyword">const</span> userLink = (</span><br><span class="line">            &lt;ul className=<span class="string">&quot;navbar-nav mr-auto&quot;</span>&gt;</span><br><span class="line">                &lt;li className=<span class="string">&quot;nav-item&quot;</span>&gt;</span><br><span class="line">                    &lt;a className=<span class="string">&quot;nav-link&quot;</span> onClick=&#123;<span class="built_in">this</span>.logout.bind(<span class="built_in">this</span>)&#125;&gt;退出&lt;/a&gt;</span><br><span class="line">                &lt;/li&gt;</span><br><span class="line">            &lt;/ul&gt;</span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">const</span> guestLinks = (</span><br><span class="line">            &lt;ul className=<span class="string">&quot;navbar-nav mr-auto&quot;</span>&gt;</span><br><span class="line">                &lt;li className=<span class="string">&quot;nav-item&quot;</span>&gt;</span><br><span class="line">                    &lt;Link className=<span class="string">&quot;nav-link&quot;</span> to=<span class="string">&quot;/signup&quot;</span>&gt;注册&lt;/Link&gt;</span><br><span class="line">                &lt;/li&gt;</span><br><span class="line">                &lt;li className=<span class="string">&quot;nav-item&quot;</span>&gt;</span><br><span class="line">                    &lt;Link className=<span class="string">&quot;nav-link&quot;</span> to=<span class="string">&quot;/login&quot;</span>&gt;登录&lt;/Link&gt;</span><br><span class="line">                &lt;/li&gt;</span><br><span class="line">            &lt;/ul&gt;</span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">return</span> (</span><br><span class="line">            &lt;nav className=<span class="string">&quot;navbar navbar-expand-lg navbar-light bg-light mb-3&quot;</span>&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;container&quot;</span>&gt;</span><br><span class="line">                    &lt;Link className=<span class="string">&quot;navbar-brand&quot;</span> to=<span class="string">&quot;/&quot;</span>&gt;Login功能&lt;/Link&gt;</span><br><span class="line">                    &lt;button className=<span class="string">&#x27;navbar-toggler&#x27;</span> type=<span class="string">&quot;button&quot;</span> data-toggle=<span class="string">&quot;collapse&quot;</span>&gt;</span><br><span class="line">                        &lt;span className=<span class="string">&quot;navbar-toggler-icon&quot;</span>&gt;&lt;/span&gt;</span><br><span class="line">                    &lt;/button&gt;</span><br><span class="line">                    &lt;div className=<span class="string">&quot;collapse navbar-collapse&quot;</span> id=<span class="string">&quot;navbarsExample05&quot;</span>&gt;</span><br><span class="line">                        &#123;isAuth?userLink:guestLinks&#125;</span><br><span class="line">                    &lt;/div&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/nav&gt;</span><br><span class="line">        )</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//取我们auth 的reducer 看看是否登录</span></span><br><span class="line"><span class="keyword">const</span> mapStateToProps = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        auth: state.auth</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(mapStateToProps,&#123;logout&#125;)(Navigator)</span><br></pre></td></tr></table></figure>
<p><img src="3.gif" alt=""></p>
<h2 id="权限"><a href="#权限" class="headerlink" title="权限"></a>权限</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 这里我们会有一个 商品的页面 如果你登录情况下我们才会让你显示</span><br><span class="line"><span class="number">2.</span> 这里我们用高阶组件去完成</span><br><span class="line"><span class="number">3.</span> 高阶组件 是一个函数 参数是一个组件 返回值是一个组件</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//requireAuth js文件</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="comment">//这里与redux建立连接 主要是要去我们又没又登录</span></span><br><span class="line"><span class="keyword">import</span> &#123;connect&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="comment">//如果没有登录 我们要有消息提示 </span></span><br><span class="line"><span class="keyword">import</span> &#123;addFlshMessage &#125; <span class="keyword">from</span> <span class="string">&#x27;../actions/flashMessages&#x27;</span></span><br><span class="line"><span class="comment">//当我们去 shop页面的时候 如果没有权限 我们就需要跳转 让它去 登录页</span></span><br><span class="line"><span class="keyword">import</span> &#123;withRouter&#125;<span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="comment">//高阶组件 是一个函数 参数 是一个组件 这里我们要把我们的shopPage组件传进来</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> (<span class="params">Compost</span>)</span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Authenticate</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">        <span class="comment">//初始的时候 就看你有么有登录 如果没有登录 就提示 如果你登录了 就让你去首页</span></span><br><span class="line">        <span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="built_in">this</span>.props.isauth,<span class="string">&#x27;---&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span>(!<span class="built_in">this</span>.props.isauth)&#123;</span><br><span class="line">                <span class="built_in">this</span>.props.addFlshMessage(&#123;</span><br><span class="line">                    type:<span class="string">&#x27;danger&#x27;</span>,</span><br><span class="line">                    text:<span class="string">&#x27;请先登录&#x27;</span></span><br><span class="line">                &#125;)</span><br><span class="line">                <span class="built_in">this</span>.props.history.push(<span class="string">&#x27;/&#x27;</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//当我们的已经登录了 在shopu页面 这个时候我们点击退出了 状态更新了 所以我们这里还需要判断已经 你的登录是否在不在了 如果不在了 我们就让你去 login页面 效果看下图</span></span><br><span class="line">        <span class="function"><span class="title">componentWillUpdate</span>(<span class="params">nextProps</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(!nextProps.isauth)&#123;</span><br><span class="line">                <span class="built_in">this</span>.props.history.push(<span class="string">&#x27;/login&#x27;</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="title">render</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span>(</span><br><span class="line">                &lt;Compost &#123;...this.props&#125;/&gt;</span><br><span class="line">            )</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//去我们的reducer里去到登录信息</span></span><br><span class="line">    <span class="keyword">const</span> mapStateToProps = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            isauth:state.auth.isAuth</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//最后导出了我们这个组件 </span></span><br><span class="line">    <span class="keyword">return</span> withRouter(connect(mapStateToProps,&#123;addFlshMessage&#125;)(Authenticate))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由文件</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;Route&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./components/App&#x27;</span></span><br><span class="line"><span class="keyword">import</span> SignupPage <span class="keyword">from</span> <span class="string">&#x27;./components/signup/SignupPage&#x27;</span></span><br><span class="line"><span class="keyword">import</span> LoginPage <span class="keyword">from</span> <span class="string">&#x27;./components/login/LoginPage&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ShopPage <span class="keyword">from</span> <span class="string">&#x27;./components/shop/ShopPage&#x27;</span></span><br><span class="line"><span class="comment">//引入我们的高阶组件</span></span><br><span class="line"><span class="keyword">import</span> requireAuth <span class="keyword">from</span> <span class="string">&#x27;./utils/requireAuth&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;Route path=<span class="string">&quot;/&quot;</span> exact component=&#123;App&#125;&gt;&lt;/Route&gt;   </span><br><span class="line">        &lt;Route path=<span class="string">&quot;/signup&quot;</span> exact component=&#123;SignupPage&#125;&gt;&lt;/Route&gt; </span><br><span class="line">        &lt;Route path=<span class="string">&quot;/login&quot;</span> exact component=&#123;LoginPage&#125;&gt;&lt;/Route&gt;  </span><br><span class="line">        &#123;<span class="comment">/*使用我们的高阶组件包裹一下*/</span>&#125;</span><br><span class="line">        &lt;Route path=<span class="string">&quot;/shop&quot;</span> exact component=&#123;requireAuth(ShopPage)&#125;&gt;&lt;/Route&gt;  </span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<p><img src="4.gif" alt=""></p>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>React 登录注册逻辑</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2019/03/23/react%20%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C/">http://example.com/2019/03/23/react%20%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2019-03-23</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/React-%E5%AE%9E%E6%88%98/">React 实战</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/03/23/React%E7%BB%84%E4%BB%B6%E7%AF%87/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">React高阶应用</div></div></a></div><div class="next-post pull-right"><a href="/2019/03/22/%E6%B5%8F%E8%A7%88%E5%99%A8%E5%BA%95%E5%B1%82%E6%B8%B2%E6%9F%93%E6%9C%BA%E5%88%B6/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">浏览器底层渲染机制</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#React-%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E9%80%BB%E8%BE%91"><span class="toc-text">React 登录注册逻辑</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="toc-text">创建项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#redux-%E5%92%8C-reducer%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-text">redux 和 reducer的配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E7%9A%84%E9%85%8D%E7%BD%AE-amp-%E5%AF%BC%E8%88%AA%E9%85%8D%E7%BD%AE"><span class="toc-text">路由的配置 &amp; 导航配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E5%86%8C%E9%A1%B5%E9%9D%A2"><span class="toc-text">注册页面</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E5%86%8C%E7%9A%84action%E9%85%8D%E7%BD%AE"><span class="toc-text">注册的action配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E5%86%8C%E6%B5%81%E7%A8%8B"><span class="toc-text">注册流程</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E5%86%8C%E6%88%90%E5%8A%9F%E6%88%96%E5%A4%B1%E8%B4%A5%E7%9A%84%E6%8F%90%E7%A4%BA"><span class="toc-text">注册成功或失败的提示</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%8F%90%E7%A4%BA"><span class="toc-text">删除提示</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BE%93%E5%85%A5%E7%94%A8%E6%88%B7%E5%90%8D%E5%A4%B1%E5%8E%BB%E7%84%A6%E7%82%B9%E7%9A%84%E6%97%B6%E5%80%99%E9%AA%8C%E8%AF%81"><span class="toc-text">输入用户名失去焦点的时候验证</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%99%BB%E5%BD%95"><span class="toc-text">登录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%BC%E8%88%AA%E7%9A%84%E5%8A%A8%E6%80%81%E6%98%BE%E7%A4%BA"><span class="toc-text">导航的动态显示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%83%E9%99%90"><span class="toc-text">权限</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>